package com.derbysoft.lowcostadapter.commons.model;

import com.derbysoft.lowcostadapter.commons.redis.service.RedisSerializable;
import com.derbysoft.lowcostadapter.commons.redis.utils.KeyValueHelper;
import com.derbysoft.lowcostadapter.dswitch2.translator.impl.MealBasis;

import java.math.BigDecimal;

public class RateCache implements RedisSerializable {

    public static final String PREFIX = "RT";

    public static final int SCALE = 2;

    private String propertyID;

    private String startDate;

    private String endDate;

    private int duration;

    private int adults;

    private int children;

    private int propertyRoomTypeID;

    private MealBasis mealBasis;

    private BigDecimal rate;

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public int getPropertyRoomTypeID() {
        return propertyRoomTypeID;
    }

    public String getPropertyID() {
        return propertyID;
    }

    public void setPropertyID(String propertyID) {
        this.propertyID = propertyID;
    }

    public void setPropertyRoomTypeID(int propertyRoomTypeID) {
        this.propertyRoomTypeID = propertyRoomTypeID;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public int getAdults() {
        return adults;
    }

    public void setAdults(int adults) {
        this.adults = adults;
    }

    public int getChildren() {
        return children;
    }

    public void setChildren(int children) {
        this.children = children;
    }

    public MealBasis getMealBasis() {
        return mealBasis;
    }

    public void setMealBasis(MealBasis mealBasis) {
        this.mealBasis = mealBasis;
    }

    public BigDecimal getRate() {
        return rate;
    }

    public void setRate(BigDecimal rate) {
        this.rate = rate;
    }

    @Override
    public String toKey() {
        return PREFIX + KeyValueHelper.SEPARATOR + getPropertyID()
                + KeyValueHelper.SEPARATOR + getStartDate()
                + KeyValueHelper.SEPARATOR + getAdults()
                + KeyValueHelper.SEPARATOR + getChildren();
    }

    @Override
    public String toFiled() {
        return getMealBasis().getMealBaseID() + KeyValueHelper.SEPARATOR + getPropertyRoomTypeID();
    }

    @Override
    public String value() {
        return getRate().toString();
    }

    @Override
    public Long expireTime() {
        return null;
    }

    public static RedisSerializable of(String key, String field, String value) {
        return build(key, field, value);
    }

    public static RateCache build(String key, String field, String value) {
        RateCache rateCache = new RateCache();
        rateCache.setPropertyID(key.split(KeyValueHelper.SEPARATOR)[1]);
        rateCache.setStartDate(key.split(KeyValueHelper.SEPARATOR)[2]);
        rateCache.setAdults(Integer.parseInt(key.split(KeyValueHelper.SEPARATOR)[3]));
        rateCache.setChildren(Integer.parseInt(key.split(KeyValueHelper.SEPARATOR)[4]));

        rateCache.setMealBasis(MealBasis.fromID(Integer.parseInt(field.split(KeyValueHelper.SEPARATOR)[0])));
        rateCache.setPropertyRoomTypeID(Integer.parseInt(field.split(KeyValueHelper.SEPARATOR)[1]));

        rateCache.setRate(new BigDecimal(value));
        return rateCache;
    }
}
